<?php
class Controller_Admin_CPC extends Controller_Admin_CPS
{
    public function __construct($req, $res)
    {
        parent::__construct($req, $res);
    }

    public function action_channels($attr_type = 'auto')
    {
        $this->template->set_filename($this->template_channels_name()); 

        $channels = Promotion_Channel_CPC::auto_cpc_ads_channels();
		$rs = array();
		foreach($channels as $key => $val)
		{
			$rs[$key] = $val;
			$tmp_promotion = new Promotion_CPC(0, $val['id']);
			$rs[$key]['cpc_ratio'] = $tmp_promotion->ratio();
		}
        $this->template->set('channels', $rs);
        $this->template->set('channels_type', $attr_type);
    }

    public function action_add_auto_channel ()
    {
        $post = $this->request->post();
        if (empty($post))
        {
            $this->template->set('games', Promotion_Activity_CPS::activities());
            $this->template->set_filename('smarty:channel/CPC/add'); 
        }
        else
        {
            $name = trim(Arr::get($post, 'channel_name'));
            $key1 = trim(Arr::get($post, 'union_channel_key'));
            $key2 = trim(Arr::get($post, 'yyh_channel_key'));
            $desc = trim(Arr::get($post, 'desc', ''));
            $type = trim(Arr::get($post, 'chntype', ''));

            if ($type == 'new')
                $key = 'ac.union.'.$key1;
            else
                $key = $key2;

            if (! empty($type) && ! empty($name) && ! empty($key))
            {
                Promotion_Channel_CPC::add_channel($name, $key, $desc);
            }

            $this->request->redirect('/CPC/channels');
        }
    }

    //
    // 重置CPC渠道的渠道系数
    //
    // @param   int     $channel_id
    // @param   int     $ratio （比如30代表 30%)
    public function action_ratio ($channel_id, $ratio)
    {
        $this->template->set_filename('smarty:util/json');
        $ratio = (int) max(0, $ratio);
        $chnid = (int) max(0, $channel_id);
        $result = NULL;

        try {
            if (empty($chnid))
                throw new Exception("invalid param");

            $promotion = new Promotion_CPC(0, $channel_id);
            $promotion->ratio($ratio);
            
            $promotion = new Promotion_CPC(0, $channel_id);
            $ratio     = $promotion->ratio();
            $result = array('status' => 0, 'ratio' => $ratio);
        }
        catch (Exception $e)
        {
            $result = array('status' => 1, 'message' => 'invalid param');
        }

        $this->template->set('json', json_encode($result));
    }



    protected function get_report ($type, $day1, $day2)
    {
        return Promotion_Effect_CPC::factory($type)->report($day1, $day2);
    }

    protected function template_report_name ()
    {
        if (site::type() == 'merchant')
            return 'smarty:effect/CPC/merchant/report';
        else
            return 'smarty:effect/CPC/report';
    }

    protected function template_channels_name ()
    {
        return 'smarty:channel/CPC/lists';
    }

    protected function report_summary (& $items)
    {
        $sum = array();
        foreach ($items as & $item)
        {
            if (! isset($item['downloads']))
                continue;

            foreach ($item['downloads'] as $k => $v)
            {
                if (! isset($sum[$k]))
                    $sum[$k] = 0;

                $sum[$k] += $v;
            }
        }

        return $sum;
    }


}
